/**
 * @author harae Feng
 * @description API 返回封装
 * @date 2019/09/11
 */
const moment = require('moment')

// 10000+ 通用错误
const error = {
    0: '成功',
    1: '失败',
    10000: '参数不全，请查阅文档(https://www.showdoc.cc/thirdapi)',
    10001: '时间格式错误(time_start,time_end)',
    10002: '参数不正确，请查阅文档(https://www.showdoc.cc/thirdapi)',
    10003: '外部接口错误',
    10004: '字段错误',
    10005: '暂无数据',
    10006: '程序错误',
    
    // 登录类
    11000: '用户尚未登录或丢失登录信息，请重新登录',

    //文件类
    11050: '文件不存在',

    // 通知类
    12000: '邮件发送失败',
    12001: '短信发送失败',
    

}

function _msg(code, parms) {
    let msg = '暂未定义错误msg'

    if(!error[code]){
        return msg
    }

    msg = error[code]

    if(parms){
        msg = `${msg}: ${parms}`
    }

    return msg
}

function ResultAPI (code=0, args={}, data) {
    this.link = {}
    this.message = _msg(code, args.parms || args.params)
    this.sendingtime = moment().format('YYYY-MM-DD HH:mm:ss')
    this.code = code
    this.success = code === 0
    this.data = data || []
}

module.exports = ResultAPI